package lib.quasar.base.loading;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ClipDrawable;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.ProgressBar;

import lib.quasar.context.BaseApp;
import lib.quasar.util.DimenUtil;
import lib.quasar.widget.bold.TextBoldView;
import lib.quasar.widget.line.LineLinearLayout;
import lib.quasar.widget.radius.RadiusLinearLayout;

public class LoadManage {

    private RadiusLinearLayout linearLayout;

    public LinearLayout getLayout() {
        if (null == linearLayout) {
            create();
        }
        return linearLayout;
    }

    /************************************************************************************/

    private final void create() {

        final Context context = BaseApp.getContext();

        linearLayout = new RadiusLinearLayout(context);
        final int width = DimenUtil.dp2px(200);
        final int height = DimenUtil.dp2px(150);
        FrameLayout.LayoutParams params1 = new FrameLayout.LayoutParams(width, height);
        params1.gravity = Gravity.CENTER;
        linearLayout.setNormalBackgroundColor(Color.parseColor("#ff9e21"));
        linearLayout.setPressBackgroundColor(Color.parseColor("#ff9e21"));
        linearLayout.setBackgroundColor(Color.parseColor("#ff9e21"));
        linearLayout.setRadius(DimenUtil.dp2px(4));
        linearLayout.setLayoutParams(params1);
        linearLayout.setOrientation(LineLinearLayout.VERTICAL);

        ProgressBar progressBar = new ProgressBar(context);
        final int size = DimenUtil.dp2px(30);
        LinearLayout.LayoutParams params2 = new LinearLayout.LayoutParams(size, size);
        params2.gravity = Gravity.CENTER_HORIZONTAL;
        params2.topMargin = size;
        params2.bottomMargin = size;
        progressBar.setLayoutParams(params2);
        ClipDrawable drawable = new ClipDrawable(new ColorDrawable(Color.WHITE), Gravity.LEFT, ClipDrawable.HORIZONTAL);
        progressBar.setProgressDrawable(drawable);//必须先设置到progressbar上再设置level，告诉这个drawable的宽度有多宽，这个level才能生效
        linearLayout.addView(progressBar);

        TextBoldView textView = new TextBoldView(context);
        final int height3 = DimenUtil.dp2px(90);
        LinearLayout.LayoutParams params3 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, height3);
        textView.setTextColor(Color.BLACK);
        textView.setText("正在加载中...");
        textView.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.TOP);
        textView.setLayoutParams(params3);
        textView.setTextColor(Color.WHITE);
        textView.setTextSize(DimenUtil.sp2px(6f));
        linearLayout.addView(textView);
    }

    private LoadManage() {
        create();
    }

    private static class SingletonHolder {
        private static LoadManage instance = new LoadManage();
    }

    public static LoadManage getManager() {
        return LoadManage.SingletonHolder.instance;
    }

    /************************************************************************************/
}
